/**
 * runner
 */
package com.whilefor.runner.modules.runner.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.whilefor.runner.common.config.Global;
import com.whilefor.runner.common.persistence.Page;
import com.whilefor.runner.common.web.BaseController;
import com.whilefor.runner.common.utils.StringUtils;
import com.whilefor.runner.modules.runner.entity.RunnerConsumer;
import com.whilefor.runner.modules.runner.service.RunnerConsumerService;

/**
 * 消费者管理Controller
 * @author lins
 * @version 2016-04
 */
@Controller
@RequestMapping(value = "${adminPath}/runner/runnerConsumer")
public class RunnerConsumerController extends BaseController {

	@Autowired
	private RunnerConsumerService runnerConsumerService;
	
	@ModelAttribute
	public RunnerConsumer get(@RequestParam(required=false) String id) {
		RunnerConsumer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = runnerConsumerService.get(id);
		}
		if (entity == null){
			entity = new RunnerConsumer();
		}
		return entity;
	}
	
	@RequiresPermissions("runner:runnerConsumer:view")
	@RequestMapping(value = {"list", ""})
	public String list(RunnerConsumer runnerConsumer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RunnerConsumer> page = runnerConsumerService.findPage(new Page<RunnerConsumer>(request, response), runnerConsumer); 
		model.addAttribute("page", page);
		return "modules/runner/runnerConsumerList";
	}

	@RequiresPermissions("runner:runnerConsumer:view")
	@RequestMapping(value = "form")
	public String form(RunnerConsumer runnerConsumer, Model model) {
		model.addAttribute("runnerConsumer", runnerConsumer);
		return "modules/runner/runnerConsumerForm";
	}

	@RequiresPermissions("runner:runnerConsumer:edit")
	@RequestMapping(value = "save")
	public String save(RunnerConsumer runnerConsumer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, runnerConsumer)){
			return form(runnerConsumer, model);
		}
		runnerConsumerService.save(runnerConsumer);
		addMessage(redirectAttributes, "保存消费者成功");
		return "redirect:"+Global.getAdminPath()+"/runner/runnerConsumer/?repage";
	}
	
	@RequiresPermissions("runner:runnerConsumer:edit")
	@RequestMapping(value = "delete")
	public String delete(RunnerConsumer runnerConsumer, RedirectAttributes redirectAttributes) {
		runnerConsumerService.delete(runnerConsumer);
		addMessage(redirectAttributes, "删除消费者成功");
		return "redirect:"+Global.getAdminPath()+"/runner/runnerConsumer/?repage";
	}

}